<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="/resources/js/jquery-1.11.1.min.js"></script>
    <script src="/resources/js/self.js"></script>
    <script type="text/javascript" src="/resources/js/md5.js"></script>
    <script type="text/javascript" src="/resources/My97DatePicker/WdatePicker.js"></script>
    <script type="text/javascript" src="/resources/js/ajaxfileupload.js"></script>
    <script type="text/javascript" src="/resources/js/Util.js"></script>

</head>
<body>
<div class="row">
    <div class="col-md-12 col-sm-12 col-xs-12">
        <div class="x_panel">
            <div class="x_content">
                <br>
                <div id="demo-form2"  class="form-horizontal form-label-left" >
                    <input type="hidden" name="id" class="form-data" >
                    <div class="form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="name">名称
                        </label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                            <input type="text" id="name" name="name"
                                   class="form-control form-data col-md-7 col-xs-12" >
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="fileUpload">图示
                        </label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                            <input type="file" name="file" multiple="true" accept="image/*" id="fileUpload" style="display:none"
                                   onchange="uploadFile('fileUpload','image', 'imgDiv', 100, '','image')"/>
                            <div id="imgDiv" style="padding-top: 23px">
                                <img src="${data.tdImage}" style="cursor: pointer; width:100px;" onclick="deleteImage(event,this)" title="" />
                            </div>
                        </div>
                    </div>


                    <!--<div class="form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="unit">单位
                        </label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                            <input type="text" id="unit" name="unit"
                                   class="form-control form-data col-md-7 col-xs-12" >
                        </div>
                    </div>-->
                    <div class="form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12">危化品分类</label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                            剧毒:<input type="checkbox" name="mProductClass" value="剧毒">
                            易制毒:<input type="checkbox" name="mProductClass" value="易制毒">
                            易制爆:<input type="checkbox" name="mProductClass" value="易制爆">
                            易爆:<input type="checkbox" name="mProductClass" value="易爆">
                            易燃:<input type="checkbox" name="mProductClass" value="易燃">
                        </div>
                    </div>


                    <div class="form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12">产品属性
                        </label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                            <textarea class="form-control form-data" name="productAttr" rows="5" placeholder=""></textarea>
                        </div>
                    </div>
                    <div class="ln_solid"></div>
                    <div class="form-group">
                        <div class="col-md-6 col-sm-6 col-xs-12 col-md-offset-3">
                            <button onclick="back()" class="btn btn-primary" type="button">返回</button>
                        </div>
                    </div>

                </div>
            </div>
        </div>
    </div>
</div>
</body>
<script type="text/javascript">
    $(function () {
        setTitle("基本信息>原材料");
        initData();
    });

    function initData(){
        var data = getData();
        var id = data.id;
        if(null2empty(id) !==""){
            $("input[name='id']").val(id);
            $.ajax({
                url:"/admin/material/find.do",
                data:{'id':id},
                type:'post',
                dataType:'json',
                success:function(data){
                    console.log(data);
                    if(data.success){
                        initPageDate(data.data, '.form-data');
                        $("#imgDiv img").attr("src",data.data.icon);
                        var mProductClass = data.data.mProductClass
                        if(!isNullOrUndefined(mProductClass)){
                            var mProductClassArray =  mProductClass.split(",");
                            $.each($('input[name="mProductClass"]:checkbox'),function(){
                                var value = $(this).val();
                                if($.inArray(value, mProductClassArray)>=0){
                                    $(this).attr('checked',true);
                                }


                            });
                        }


                        /*if(!isNullOrUndefined(data.data.icon)){
                         var imgArray =  new Array();
                         imgArray.push({"id":data.data.icon,"path":data.data.icon});
                         addFileInfoToTable(imgArray);
                         }*/
                    }
                }
            });
            $("#account").attr('readonly','readonly');
        }

    }

    function save() {
        var params = getPageDataByClass("form-data");
        if (strIsNull(params['name'])) {
            alert("请输入危险品名称");
            return;
        }
        var img = $("#imgDiv img");
        if(img.length > 0) {
            params['icon'] = $(img[0]).attr("src");
        }else{
            alert("请上传首图");
            return;
        }
        var mProductClass = "";
        $.each($('input[name="mProductClass"]:checkbox:checked'),function(){
            if(mProductClass === ""){
                mProductClass = $(this).val();
            }else {
                mProductClass = mProductClass+"," +$(this).val();
            }

        });
        params['mProductClass'] =mProductClass;
        $.ajax({
            url: "/admin/material/save.do",
            data: params,
            dataType: 'json',
            type: 'post',
            success: function (data) {
                console.log(data);
                if (data.success) {
                    alert("保存成功");
                    goto("material_list.html");
                } else {
                    alert(data.msg);
                }
            }
        });
    }
    function back() {
        goto("material_list.html");
    }
</script>
</html>